#!/usr/bin/env python3
import xlrd
from datetime import datetime, date
import sys
import os
import glob
dirs = ("A-品牌部分","B-产品功能","C-数学学科","D-英语学科","E-语文学科","F-理化学科", "G-小学科", "H-优惠福利")

if len(sys.argv) != 3:
    print('	{} [input.excel] [start-row-number]'.format(sys.argv[0]))
    sys.exit(-1)

start = int(sys.argv[2]) - 1

rb = xlrd.open_workbook(sys.argv[1])
sheet = rb.sheet_by_index(0)
rows = sheet.nrows
cols =  sheet.ncols
total =  rows - start
#0 empty 1 string 2 number 3 date
#for x in range(start, rows):
allfiles = list()

for x in range(start, start + total):
    test_date = ""
    test_date_month = 0
    test_date_day = 0
    test_date_year = 2020
    first_name, last_name = "", ""
    birth = ""
    tel = ""
    email = ""
    price = 0
    start = ord('A')

   # for y in range(cols):
    ins = {"liveid":"", "files":"", "targets":list()}
    if True:
        y = (ord('K')  - start)
        cell = sheet.cell(x, y)
        liveid = cell.value
        ins['liveid'] = liveid

        y = (ord('J')  - start)
        cell = sheet.cell(x, y)
        files = cell.value
        ins['files'] = files
        allfiles.append(ins)
        t = files.split('-')  
        for w,m in enumerate(t):
            print(w,m, dirs[w])
            search = glob.glob(dirs[w] + "/*")
            for f in search:
                if f.find(m + "-") >= 0:
                    ins['targets'].append(f)
           
 
       

for x in allfiles:
    filename = x['liveid'] + ".mp4"
    targets = x['targets']
    if os.path.exists(os.path.join("output", filename)):
        print("existed")
        continue
    print(filename, targets)
    files = " ".join(targets)
    cmd = "./concat " + files
    print(cmd)
    os.system(cmd)
    os.rename("output.mp4", "output/"+filename)
     
       

